package com.estore.managerImpl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;

import com.estore.converter.BlankRemover;
import com.estore.converter.DateTime;
import com.estore.dao.Product_Dao;
import com.estore.dao.ShopDao;
import com.estore.entity.AgentBanner;
import com.estore.entity.Agent_Product;
import com.estore.entity.Agent_Profile;
import com.estore.entity.Brand;
import com.estore.entity.Categories;
import com.estore.entity.Discount;
import com.estore.entity.MainCategories;
import com.estore.entity.ProductList;
import com.estore.entity.ShopInterface;
import com.estore.info.AgentProductInfo;
import com.estore.info.DiscountInfo;
import com.estore.info.ProductImageInfo;
import com.estore.info.ProductInfo;
import com.estore.info.ShopInfo;
import com.estore.info.SortInfo;
import com.estore.manager.ShopManager;

public class ShopManagerImpl implements ShopManager {
	private ShopDao shopDao;
	private Product_Dao productDao;
	public HttpServletRequest request;
	DateTime dateTime = new DateTime();

	@Override
	public boolean checkAgentAvail(Integer userID) {
		if (shopDao.checkAgentAvail(userID)) {
			return true;
		} else
			return false;
	}

	public ShopDao getShopDao() {
		return shopDao;
	}

	public void setShopDao(ShopDao shopDao) {
		this.shopDao = shopDao;
	}

	@Override
	public void regShop(ShopInfo info) {
		shopDao.regShop(info);

	}

	@Override
	public boolean checkAgentNameAvail(String agentName) {
		if (shopDao.checkAgentNameAvail(agentName)) {
			return true;
		} else
			return false;
	}

	@Override
	public boolean checkEmailAvail(String email) {
		if (shopDao.checkEmailAvail(email)) {
			return true;
		} else
			return false;
	}

	@Override
	public boolean checkPhoneNumberAvail(String phoneNumber) {
		if (shopDao.checkPhoneNumberAvail(phoneNumber)) {
			return true;
		} else
			return false;
	}

	@Override
	public ShopInfo getShopInfo(Integer userID) {
		ShopInfo info = shopDao.getShopInfo(userID);
		return info;
	}

	@Override
	public void changeShopProfile(ShopInfo info) {
		shopDao.changeShopProfile(info);

	}

	@Override
	public void updateLogo(String logo, Integer agentID) {
		shopDao.updateLogo(logo, agentID);

	}

	@Override
	public void addProductInfoByShop(Integer productID, AgentProductInfo agentProduct, Integer saleType, Integer AgentID) {
		agentProduct.setProductID(productID);
		agentProduct.setListType(saleType);
		agentProduct.setAgentID(AgentID);
		String date = dateTime.currentDate();
		agentProduct.setStartDate(date);
		shopDao.newList(agentProduct);
		shopDao.saleProductByShop(agentProduct);
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public DateTime getDateTime() {
		return dateTime;
	}

	public void setDateTime(DateTime dateTime) {
		this.dateTime = dateTime;
	}

	@Override
	public boolean checkListAvail(Integer productID, Integer agentID) {
		if (shopDao.checkListAvail(productID, agentID)) {
			return true;
		}
		return false;
	}

	@Override
	public List<ProductList> getAllProductOfShop(Integer agentID) {
		List<ProductList> listShopProduct = shopDao
				.getAllProductOfShop(agentID);
		return listShopProduct;
	}

	@Override
	public Agent_Product getAgentProductInfo(Integer listID) {
		Agent_Product info = shopDao.getAgentProductInfo(listID);
		return info;
	}

	@Override
	public List<AgentProductInfo> getListAgentSellingProduct(Integer productID) {
		// get all selling price of product
		List<AgentProductInfo> listAgentInfo = shopDao.getAgentProductByProductID(productID);
		// get all discount of product
		List<Discount> listDiscount = shopDao.getAllDiscountByProductID(productID);
		List<SortInfo> listSort = new ArrayList<SortInfo>();
		if(!listDiscount.isEmpty()){
			// set all discount to list sort
			for(Discount record : listDiscount){
				SortInfo info = new SortInfo();
				info.setListID(record.getListID());
				info.setSort(record.getDiscount());
				listSort.add(info);
			}
		}
		if(!listAgentInfo.isEmpty()){
			// set all selling price to list sort
			for(AgentProductInfo record : listAgentInfo){
				SortInfo info = new SortInfo();
				boolean check = true;
				if(!listSort.isEmpty()){
					for(SortInfo record1 : listSort){
						if(record1.getListID() == record.getListID()){
							check = false;
						}
					}
				}
				// if product have discount then ignore this
				if(check == true){
					info.setListID(record.getListID());
					info.setSort(record.getSellingPrice());
					listSort.add(info);
				}
			}
		}
		// sort
		Vector<SortInfo> vSort = new Vector<SortInfo>();
		if(!listSort.isEmpty()){
			for(SortInfo record : listSort){
				vSort.add(record);
			}
		}
		Collections.sort(vSort, new Comparator<SortInfo>() {

			@Override
			public int compare(SortInfo o1, SortInfo o2) {
				return new Integer(o1.getSort().compareTo(o2.getSort()));
			}
		});
		
		List<AgentProductInfo> list = new ArrayList<AgentProductInfo>();
		for (SortInfo record : vSort) {
			AgentProductInfo info = new AgentProductInfo();
			ProductList productList = shopDao.getProductList(record.getListID());
			// get anget product by listid
			Agent_Product agentProduct = shopDao.getAgentProductInfo(record.getListID());
			if(shopDao.getDiscount(record.getListID()) != null){
				// get discount by listid
				Discount discount = shopDao.getDiscount(record.getListID());
				float discountPercent = Math.round((float)((((float)agentProduct.getSellingPrice() - (float)discount.getDiscount())  / 
						(float)agentProduct.getSellingPrice())) * 100);
				info.setDiscount(discount.getDiscount());
				info.setDiscountPercent(discountPercent);
			}
			Agent_Profile profile = shopDao.getAgentProfileSellingProduct(productList.getAgentID());
			info.setWarranty(agentProduct.getWarranty());
			info.setCondition(agentProduct.getProductCondition());
			info.setSellingPrice(agentProduct.getSellingPrice());
			info.setAgentName(profile.getAgentName());
			info.setLogo(profile.getLogo());
			info.setListID(productList.getListID());
			info.setAgentID(profile.getAgentID());
			list.add(info);
		}
		return list;
	}

	@Override
	public List<Agent_Profile> getAllAgent() {
		List<Agent_Profile> listProfile = shopDao.getAllAgent();
		return listProfile;
	}

	@Override
	public ShopInfo getShopInfoByAgentID(Integer agentID) {
		ShopInfo info = shopDao.getShopInfoByAgentID(agentID);
		return info;
	}

	@Override
	public AgentProductInfo getLowestPrice(Integer productID) {
		AgentProductInfo agentProductInfo = shopDao.getLowestPrice(productID);
		
		// if have discount match lowest price, return discount as lowest price
		if(shopDao.getLowestDiscount(productID) != null){
			DiscountInfo discountInfo = shopDao.getLowestDiscount(productID);
			AgentProductInfo lowestPrice = new AgentProductInfo();
			if(discountInfo.getDiscount() < agentProductInfo.getSellingPrice()){
				Agent_Product agentProduct = shopDao.getAgentProductInfo(discountInfo.getListID());
				Discount discount = shopDao.getDiscount(discountInfo.getListID());
				lowestPrice.setSellingPrice(agentProduct.getSellingPrice());
				lowestPrice.setListID(discountInfo.getListID());
				lowestPrice.setDiscount(discount.getDiscount());
				
				// set discount percent
				float discountPercent = Math.round((float)((((float)agentProduct.getSellingPrice() - (float)discount.getDiscount())  / 
						(float)agentProduct.getSellingPrice())) * 100);
				lowestPrice.setDiscountPercent(discountPercent);
			}else {
				Agent_Product agentProduct = shopDao.getAgentProductInfo(agentProductInfo.getListID());
				lowestPrice.setSellingPrice(agentProduct.getSellingPrice());
				lowestPrice.setListID(discountInfo.getListID());
				if(shopDao.getDiscount(discountInfo.getListID()) != null){
					Discount discount = shopDao.getDiscount(discountInfo.getListID());
					lowestPrice.setDiscount(discount.getDiscount());
					
					// set discount percent
					float discountPercent = Math.round((float)((((float)agentProduct.getSellingPrice() - (float)discount.getDiscount())  / 
							(float)agentProduct.getSellingPrice())) * 100);
					lowestPrice.setDiscountPercent(discountPercent);
				}
			}
			return lowestPrice;
		}
		return agentProductInfo;
	}


	@Override
	public void setDefaultInterface(Integer agentID) {
		shopDao.setDefaultInterface(agentID);
		
	}

	@Override
	public Integer getAgentID(String agentName) {
		Integer agentID = shopDao.getAgentID(agentName);
		return agentID;
	}

	@Override
	public ShopInterface getShopInterface(Integer agentID) {
		ShopInterface shopInterface = shopDao.getShopInterface(agentID);
		return shopInterface;
	}

	@Override
	public void editBanner(ShopInterface shop) {
		shopDao.editBanner(shop);
		
	}

	@Override
	public void editCategoriesBar(ShopInterface shop) {
		shopDao.editCategoriesBar(shop);
		
	}

	@Override
	public void editProductType(ShopInterface shop) {
		shopDao.editProductType(shop);
		
	}

	@Override
	public void editTemplate(ShopInterface shop) {
		shopDao.editTemplate(shop);
		
	}

	@Override
	public List<AgentBanner> listAgentBanner(Integer agentID) {
		List<AgentBanner> listBanner = new ArrayList<AgentBanner>();
		if(shopDao.listAgentBanner(agentID) != null){
			listBanner = shopDao.listAgentBanner(agentID);
			return listBanner;
		}
		return null;
	}

	@Override
	public void deleteBanner(Integer agentID, String linkBanner) {
		shopDao.deleteBanner(agentID, linkBanner);
		
	}

	@Override
	public void addShopBanner(Integer agentID, List<String> listLinkBanner) {
		for(String record : listLinkBanner){
			shopDao.addShopBanner(agentID, record);
		}
		
		
	}

	@Override
	public void setBannerTittle(AgentBanner banner) {
		shopDao.setBannerTittle(banner);
		
	}

	@Override
	public ProductList getProductList(Integer productID, Integer agentID) {
		ProductList productList = shopDao.getProductList(productID, agentID);
		return productList;
	}

	@Override
	public void changeProductInfo(AgentProductInfo info) {
		shopDao.changeProductInfo(info);
		
	}

	@Override
	public ProductList getProductList(Integer listID) {
		ProductList productList = shopDao.getProductList(listID);
		return productList;
	}

	@Override
	public void makeDiscount(Discount discount, Integer duration) {
		discount.setStartDate(dateTime.currentDay());
		discount.setEndDate(dateTime.endDate(duration));
		discount.setStatus(1);
		shopDao.makeDiscount(discount);
	}

	@Override
	public boolean checkDiscountAvailable(Integer listID) {
		if(shopDao.checkDiscountAvailable(listID)){
			return true;
		}
		return false;
	}

	@Override
	public Discount getDiscount(Integer listID) {
		Discount discount = shopDao.getDiscount(listID);
		return discount;
	}

	@Override
	public void editDiscount(Discount discount) {
		shopDao.editDiscount(discount);
		
	}

	
	// pending
	@Override
	public List<ProductInfo> getList12MostDiscountProduct() {
		List<Integer> listProductID = shopDao.getListDiscountAvail();
		List<AgentProductInfo> listAgentProductInfo = new ArrayList<AgentProductInfo>();
		
		// get list listID, discount, selling price of product list have discount and discount highest 
		for(Integer record : listProductID){
			// get most discount of each product
			AgentProductInfo agentProductInfo = shopDao.getMostDiscountByProductID(record);
			listAgentProductInfo.add(agentProductInfo);
		}
		return null;
	}

	public Product_Dao getProductDao() {
		return productDao;
	}

	public void setProductDao(Product_Dao productDao) {
		this.productDao = productDao;
	}

	@Override
	public String getShopAutoComplete(Integer agentID) {
		List<ProductList> listProductList = shopDao.getAllProductOfShop(agentID);
		String result = "";
		if(productDao.getAllMainCategories() != null){
			List<MainCategories> listMainCategories = productDao.getAllMainCategories();
			for(MainCategories record: listMainCategories){
				if(record.equals(listMainCategories.get(listMainCategories.size()-1))){
					result += '"' + record.getMainCatName()+ '"';
				}else {
					result += '"' + record.getMainCatName() + '"' + ", ";
				}
			}
			result += ", ";
		}
		if(productDao.listAllCategories() != null){
			List<Categories> listCategories = productDao.listAllCategories();
			for(Categories record: listCategories){
				if(record.equals(listCategories.get(listCategories.size()-1))){
					result += '"' + record.getCatName()+ '"';
				}else {
					result += '"' + record.getCatName() + '"' + ", ";
				}
			}
			result += ", ";
		}
		if(productDao.getListBrand() != null){
			List<Brand> listBrand = productDao.getListBrand();
			for(Brand record: listBrand){
				if(record.equals(listBrand.get(listBrand.size()-1))){
					result += '"' + record.getBrandName()+ '"';
				}else {
					result += '"' + record.getBrandName() + '"' + ", ";
				}
			}
			result += ", ";
		}
		for(ProductList record : listProductList){
			ProductInfo productInfo = productDao.getProductInfo(record.getProductID());
			if(record.equals(listProductList.get(listProductList.size()-1))){
				result += '"' + productInfo.getCatName() + " " + productInfo.getBrandName() + " " + productInfo.getProductName()+ '"' + ", ";
				result += '"' + productInfo.getBrandName() + " " + productInfo.getProductName()+ '"';
			}else {
				result += '"' + productInfo.getCatName() + " " + productInfo.getBrandName() + " " + productInfo.getProductName()+ '"' + ", ";
				result += '"' + productInfo.getBrandName() + " " + productInfo.getProductName()+ '"' + ", ";
			}
		}
		return result;
	}

	@Override
	public List<ProductInfo> listExactlyResultForShop(String searchText, Integer agentID) {
		List<ProductInfo> listResultAll = new ArrayList<ProductInfo>();
		String text = BlankRemover.trim(searchText);
		String[]tokens = text.split("-|\\.|\\ |\\'|\\=|\\+|\\_|\\/|\\?");
		String temp = "";
		for(String record : tokens ){
			temp+= "%" + record + "% ";
		}
		String search = BlankRemover.trim(temp);
		// get all exactly result
		List<ProductInfo> listProduct = productDao.exactlySearchResult(search);
		if(!listProduct.isEmpty()){
			for(ProductInfo record : listProduct){
				ProductInfo info = productDao.getProductInfo(Integer.parseInt(record.getProductID()));
				ProductImageInfo imageInfo = productDao.getImageOfProduct(Integer.parseInt(record.getProductID()), 0);
				info.setMainImgLink(imageInfo.getMainImgLink());
				info.setListLinkImg(imageInfo.getListImgLink());
				listResultAll.add(info);
			}
		}
		List<ProductInfo> listResult = new ArrayList<ProductInfo>();
		List<ProductList> listProductList = shopDao.getAllProductOfShop(agentID);
		for(ProductList record : listProductList){
			for(ProductInfo record1 : listResultAll){
				if(Integer.parseInt(record1.getProductID()) == record.getProductID()){
					listResult.add(record1);
				}
			}
		}
		return listResult;
	}

	@Override
	public List<ProductInfo> listFullTextSearchResultForShop(String searchText,
			Integer agentID) {
		List<ProductInfo> listResultAll = new ArrayList<ProductInfo>();
		String text = BlankRemover.trim(searchText);
		String[]tokens = text.split("-|\\.|\\ |\\'|\\=|\\+|\\_|\\/|\\?");
		String temp = "";
		for(String record : tokens ){
			temp+= "%" + record + "% ";
		}
		String search = BlankRemover.trim(temp);
		// get all exactly result
		List<ProductInfo> listProduct = productDao.fullTextSearchResult(search);
		if(!listProduct.isEmpty()){
			for(ProductInfo record : listProduct){
				ProductInfo info = productDao.getProductInfo(Integer.parseInt(record.getProductID()));
				ProductImageInfo imageInfo = productDao.getImageOfProduct(Integer.parseInt(record.getProductID()), 0);
				info.setMainImgLink(imageInfo.getMainImgLink());
				info.setListLinkImg(imageInfo.getListImgLink());
				listResultAll.add(info);
			}
		}
		List<ProductInfo> listResult = new ArrayList<ProductInfo>();
		List<ProductList> listProductList = shopDao.getAllProductOfShop(agentID);
		for(ProductList record : listProductList){
			for(ProductInfo record1 : listResultAll){
				if(Integer.parseInt(record1.getProductID()) == record.getProductID()){
					listResult.add(record1);
				}
			}
		}
		return listResult;
	}

	@Override
	public void endDiscount(Integer listID) {
		shopDao.endDiscount(listID);
		
	}

	@Override
	public Agent_Profile getAgentProfile(Integer agentID) {
		Agent_Profile profile = shopDao.getAgentProfile(agentID);
		return profile;
	}

	@Override
	public void endList(Integer listID) {
		shopDao.endList(listID);
		
	}

	@Override
	public List<Agent_Profile> searchAgentByAgentName(String agentName, boolean searchType) {
		List<ProductInfo> listResult = new ArrayList<ProductInfo>();
		String text = BlankRemover.trim(agentName);
		String[]tokens = text.split("-|\\.|\\ |\\'|\\=|\\+|\\_|\\/|\\?");
		String temp = "";
		for(String record : tokens ){
			temp+= "%" + record + "% ";
		}
		String search = BlankRemover.trim(temp);
		List<Agent_Profile> listAgent = new ArrayList<Agent_Profile>();
		if(searchType == true){
			listAgent = shopDao.ListExactlyAgentName(agentName);
		}else{
			listAgent = shopDao.ListSimilarAgentName(agentName);
		}
		if(!listAgent.isEmpty()){
			return listAgent;
		}
		return null;
	}

}
